<?php if(!defined("VIT")) die("Restricted access");
/**********************************************
	- Author : VIT
	- Date : 01 - 05 - 2009
	- Company : VIT
	- Service: Web Design, Software Development
*/
	ob_start();
	
	include(dirname(__FILE__)."/config.php");
		
	$connect	= mysql_connect($host,$user,$pass);
	$data		= mysql_select_db($name,$connect);
	$unicode	= mysql_query($charset);
	if($data){
		return true;
	}
	else{
		return false;
	}
	
// Query
function query($sql){
	$res = @mysql_query($sql);
	return $res;
}

// Rows
function rows($sql){
	$row = @mysql_fetch_array(query($sql));
	return $row;
}

// Nums
function nums($sql){
	$num = @mysql_num_rows(query($sql));
	return $num;
}

// Redirect Administrator
function Redirect(){
	header("Location: index.php?p=".$_GET['p']."&q=list");
}

// Check Email
function CheckMail($mail){
	if (!ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@'.'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $mail))
		return true;
	else
		return false;
}

// Reorder
function Up($p,$i,$id) {
	if ($i > 0){
		echo '<a href="javascript: void(0)" onClick="window.location=\'index.php?p='.$p."&q=reorder&t=up&id=".$id.'\'">
				<img src="images/j_arrow_up.png" border="0" alt="Move Up" title="Move Up">
			  </a>';
	} else {
		echo '&nbsp;';
	}
}
	
function Down($p,$i,$n,$id) {
	if ($i < $n-1) {
		echo '<a href="javascript: void(0)" onClick="window.location=\'index.php?p='.$p."&q=reorder&t=down&id=".$id.'\'">
				<img src="images/j_arrow_down.png" border="0" alt="Move Down" title="Move Down">
			</a>';
	} else {
		echo '&nbsp;';
	}
}

// Date
function Day($date){
	$day = substr($date,8,2) ."-". substr($date,5,2) ."-". substr($date,0,4);
	return $day;
}
	
// Publish
function Publish($p,$t,$id) {
	if($t==1){
		$k = $t-1;
		echo '<a href="javascript: void(0)" onClick="window.location=\'index.php?p='.$p.'&q=publish&t='.$k.'&id='.$id.'\'">
				<img src="images/tick.png" alt="Published" border="0">
			</a>';
	}
	else{
		$k = $t+1;
		echo '<a href="javascript: void(0)" onClick="window.location=\'index.php?p='.$p.'&q=publish&t='.$k.'&id='.$id.'\'">
				<img src="images/publish_x.png" alt="Unpublished" border="0">
			</a>';
	}
}
	
// Rewrite Label
function Rewrite($label){
	$search = array("À","Á","Â","Ã","Ä","Å","à","á","â","ã","ä","å","Ò","Ó","Ô","Õ","Ö","Ø","ò","ó","ô","õ","ö","ø","È","É","Ê","Ë","è","é","ê","ë","Ç","ç","Ì","Í","Î","Ï","ì","í","î","ï","Ù","Ú","Û","Ü","ù","ú","û","ü","ÿ","Ñ","ñ");
	$model = array("A","A","A","A","A","A","a","a","a","a","a","a","O","O","O","O","O","O","o","o","o","o","o","o","E","E","E","E","e","e","e","e","C","c","I","I","I","I","i","i","i","i","U","U","U","U","u","u","u","u","y","N","n");
	$label = str_replace($search,$model,$label);
	$search2 = array ('@[^a-zA-Z0-9]@');
	$replace = array (' ');
	$label =  preg_replace($search2, $replace, $label); 
	
	$label = strtolower($label); // mais toutes les lettres de la chaîne en minuscule
	$label = str_replace(" ",'-',$label); // remplace les espaces en tirets
	$label = preg_replace('#\-+#','-',$label); // enlève les autres caractères inutiles
	$label = preg_replace('#([-]+)#','-',$label);
	trim($label,'-'); // remplace les espaces restants par des tirets

	return $label;
}
	
// Script Alert
function iAlert($session){
	if(isset($_SESSION[$session])){
		echo "<script> alert('".$_SESSION[$session]."'); </script>";
		unset($_SESSION[$session]);
	}
}
	
// Create Photo Thumbnail
function createThumbnail($image,$largeur_max,$hauteur_max,$source,$destination,$prefixe) {
	if (!file_exists($source.$image)) {
		echo "Error: no directory";
		exit;
	}
	$ext=strtolower(strrchr($image,'.'));
	if ($ext==".jpg" || $ext==".jpeg" || $ext==".gif" || $ext==".png" || $ext==".JPG" || $ext==".JPEG") {
		$size = getimagesize($source.$image);
		$largeur_src=$size[0];
		$hauteur_src=$size[1];
		if ($size[2]==1 || $size[2]!=2 || $size[2]!=3 || $size[2]!=6) {
			if($size[2]==1) {
				// format gif
				$image_src=imagecreatefromgif($source.$image);
			}
			if($size[2]==2) {
				// format jpg ou jpeg
				$image_src=imagecreatefromjpeg($source.$image);
			}
			if($size[2]==3) {
				// format png
				$image_src=imagecreatefrompng($source.$image);
			}
			if($size[2]==6) {
				// format bmp
				$image_src=imagecreatefromwbmp($source.$image);
			}
			if ($largeur_src>$largeur_max OR $hauteur_src>$hauteur_max) {
				if ($hauteur_src<=$largeur_src) {
					$ratio=$largeur_max/$largeur_src;
				} else {
					$ratio=$hauteur_max/$hauteur_src;
				}
			} else {
				$ratio=1;
			}
			$image_dest=imagecreatetruecolor(round($largeur_src*$ratio), round($hauteur_src*$ratio));
			imagecopyresampled($image_dest,$image_src,0,0,0,0,round($largeur_src*$ratio),round($hauteur_src*$ratio),$largeur_src,$hauteur_src);
			if(!imagejpeg($image_dest, $destination.$prefixe.$image)) {
				exit;
			}
		}
	}
}
	
// Create paging
function paging($pageNum,$num,$rowsPerPage,$objId,$objImg,$strURL){
	$maxPage = '';
	$nav = '';
	$maxPage = ceil($num/$rowsPerPage);		
	for($page = 1; $page <= $maxPage; $page++){
		if ($page == $pageNum){
			$nav .= "<label class='paging_active'>$page</label>";
		}
		else{
			if($objId!=""){
				$nav .= "<a class='paging' href=\"javascript: void(0)\" onClick=\"getContent('".$objId."','".$objImg."','".$strURL."-".$page."')\">$page</a>";
			}
			else{
				$nav .= "<a class='paging_active' href=\"". $strURL ."-". $page."\">$page</a> ";
			}
		}		
	}
	if ($pageNum > 1){
		$page = $pageNum - 1;
		if($objId!=""){
			//$prev = "<a class='paging_nnt' href=\"javascript: void(0)\" onClick=\"getContent('".$objId."','".$objImg."','".$strURL."-".$page."')\"><img src='images/prev.jpg' border='0' /></a>";
			$prev = "<a class='paging' href=\"javascript: void(0)\" onClick=\"getContent('".$objId."','".$objImg."','".$strURL."-".$page."')\"> << </a>";
		}
		else{
			$prev = "<a href=\"". $strURL ."-". $page."\"> [ "._lg_previous." ] </a> ";
		}
	} 
	else{
		$prev  = '&nbsp;';
	}
	if ($pageNum < $maxPage){
		$page = $pageNum + 1;
		if($objId!=""){
			//$next = "<a class='paging_nnt' href=\"javascript: void(0)\" onClick=\"getContent('".$objId."','".$objImg."','".$strURL ."-". $page. "')\"><img src='images/dot_new.jpg' border='0' /> </a>";
			$next = "<a class='paging' href=\"javascript: void(0)\" onClick=\"getContent('".$objId."','".$objImg."','".$strURL ."-". $page. "')\"> >> </a>";	
		}
		else{
			$next = "<a href=\"". $strURL ."-". $page. "\"> [ "._lg_next." ] </a>";	
		}
	} 
	else{
		$next  = '&nbsp;';
	}		
	echo $first . $prev . $nav . $next . $last ;
}

// Create Paging Advanced
class Page{

	var $total_records=1;  
	var $records_per_page=1;  
	var $page_name=""; 
	var $start=0; 
	var $page=0;
	var $total_page=0;
	var $current_page;
	var $remain_page;
	var $show_prev_next=true;
	var $show_scroll_prev_next=false;
	var $show_first_last=false;
	var $show_disabled_links=true;
	var $scroll_page=0;
	var $qry_str="";
	var $link_para="";
	var $jsHandler="";

	/* returns boolean value if it is last page or not*/	
	function is_last_page()
	{
		return $this->page>=$this->total_page-1?true:false;
	}
	/* param : Void
	 returns boolean value if it is first page or not*/	
	function is_first_page()
	{
		return $this->page==0?true:false;
	}
	
	/**
	 * Input : Void
	 * Return: The current page 
	 */
	function current_page()
	{
		return $this->page+1;
	}
	
	/**
	 * Return the total pages.
	 */ 
	
	function total_page()
	{
		return $this->total_page==0?1:$this->total_page;
	}
	
	//@param : $show = if you want to show desabled links on navigation links.
	//
	function show_disabled_links($show=TRUE)	
	{
		$this->show_disabled_links=$show;
	}
	//@param : $link_para = if you want to pass any parameter to link
	//
	function set_link_parameter($link_para)
	{
		$this->link_para=$link_para;
	}
	
	function set_page_name($page_name)
	{
		$this->page_name=$page_name;
	}
	
	/**
	 * Function is used to set the javascript handler function
	 * @param $jsHandler: String Javascript function name
	 * @return  void; 
	 * @uses Function will use javascript handler function for pagging. It will pass the two parameters to 
	 * javascript function. First is page name and the second one is query string.
	 */ 
	function setJSHandler($jsHandler)
	{
		$this->jsHandler = $jsHandler;
	}
	
	//@param : str= query string you want to pass to links.
	function set_qry_string($str="",$usePost = true)
	{
		$this->qry_str="&".preg_replace("/&&page=\d*/","",$str);
		if($usePost)
		{
				foreach ($_POST as $key => $value)
					if(strpos($this->qry_str,"$key=")===false)
						$this->qry_str.="&$key=$value";
				foreach ($_GET as $key => $value)
					if(strpos($this->qry_str,"$key=")===false)
						$this->qry_str.="&$key=$value";
		}
		
		$this->qry_str= "&".preg_replace("/&&page=\d*/","",$this->qry_str);
	}
	
	function set_scroll_page($scroll_num=0)
	{
		if($scroll_num!=0)
			$this->scroll_page=$scroll_num;
		else
			$this->scroll_page=$this->total_records;
	}
	
	function set_total_records($total_records)
	{
		if($total_records<0)
		  $total_records=0;
		$this->total_records=$total_records;
	}
	
	function set_records_per_page($records_per_page)
	{
		 if($records_per_page<=0)
			  $records_per_page=$this->total_records;
		 $this->records_per_page=$records_per_page;
	}
	/* @params
	* 	$page_name = Page name on which class is integrated. i.e. $_SERVER['PHP_SELF']
	*  	$total_records=Total records returnd by sql query.
	* 	$records_per_page=How many projects would be displayed at a time 
	*		$scroll_num= How many pages scrolled if we click on scroll page link. 
	* 				i.e if we want to scroll 6 pages at a time then pass argument 6.
	* 	$show_prev_next= boolean(true/false) to show prev Next Link
	* 	$show_scroll_prev_next= boolean(true/false) to show scrolled prev Next Link
	* 	$show_first_last= boolean(true/false) to show first last Link to move first and last page.
	*/
	
	function set_page_data($page_name,$total_records,$records_per_page=1,$scroll_num=0,$show_prev_next=true,$show_scroll_prev_next=false,$show_first_last=false)
	{
		$this->set_total_records($total_records);
		$this->set_records_per_page($records_per_page);
		$this->set_scroll_page($scroll_num);
		$this->set_page_name($page_name);
		$this->show_prev_next=$show_prev_next;
		$this->show_scroll_prev_next=$show_scroll_prev_next;
		$this->show_first_last=$show_first_last;
	}
	
	/* @params
	*  $user_link= if you want to display your link i.e if you want to user '>>' instead of [first] link then use
	 Page::get_first_page_nav(">>") OR for image
	 Page::get_first_page_nav("<img src='' alt='first'>") 
	 $link_para: link parameters i.e if you want ot use another parameters such as class.
	 Page::get_first_page_nav(">>","class=myStyleSheetClass")
	*/	   
	function get_first_page_nav($user_link="",$link_para="",$returnHtml=false)
	{
		if($this->total_page<=1)return;
		if(trim($user_link)=="") $user_link="[First]";
		
		if(empty($link_para)) $link_para = $this->link_para;
		
		$html = "";
		if(!$this->is_first_page()&& $this->show_first_last)
		{
			if(empty($this->jsHandler))
				$html = ' <a href="'.$this->page_name.'&page=0'.$this->qry_str.'" '.$link_para.'>'.$user_link.'</a> ';
			else 
				$html = ' <a href="javascript:void(0);" onclick="'.$this->jsHandler.'(\''.$this->page_name.'\',\'page=0'.$this->qry_str.'\')" '.$link_para.'>'.$user_link.'</a> ';
		}
		elseif($this->show_first_last && $this->show_disabled_links)
			$html = $user_link;
		
		if($returnHtml) return $html;
		
		echo $html;
		
	}
	
	function get_last_page_nav($user_link="",$link_para="",$returnHtml=false)
	{
		if($this->total_page<=1) return;
		if(trim($user_link)=="") $user_link="[Last]";
	
		if(empty($link_para)) $link_para = $this->link_para;
		
		$html = "";
	
		if(!$this->is_last_page()&& $this->show_first_last)
		{
			if(empty($this->jsHandler))
				$html = ' <a href="'.$this->page_name.'&page='.($this->total_page-1).$this->qry_str.'" '.$link_para.'>'.$user_link.'</a> ';
			else 
				$html = ' <a href="javascript:void(0);" onclick="'.$this->jsHandler.'(\''.$this->page_name.'\',\'page='.($this->total_page-1).$this->qry_str.'\')" '.$link_para.'>'.$user_link.'</a> ';
		}
		elseif($this->show_first_last && $this->show_disabled_links)
			$html =  $user_link;
	
		if($returnHtml) return $html;
		
		echo $html;
	}
	
	function get_next_page_nav($user_link="",$link_para="",$returnHtml=false)
	{
		if($this->total_page<=1) return;
		if(trim($user_link)=="") $user_link=" Next &raquo;";
		if(empty($link_para)) $link_para = $this->link_para;
	
		$html = "";
		if(!$this->is_last_page()&& $this->show_prev_next)
		{
			if(empty($this->jsHandler))
				$html = ' <a href="'.$this->page_name.'&page='.($this->page+1).$this->qry_str.'" '.$link_para.'>'.$user_link.'</a> ';
			else 
				$html = ' <a href="javascript:void(0);" onclick="'.$this->jsHandler.'(\''.$this->page_name.'\',\'page='.($this->page+1).$this->qry_str.'\')" '.$link_para.'>'.$user_link.'</a> ';
		}
		elseif($this->show_prev_next && $this->show_disabled_links)
			$html = $user_link;
	
		if($returnHtml) return $html;
		
		echo $html;
	}
	
	function get_prev_page_nav($user_link="",$link_para="",$returnHtml=false)
	{
		if($this->total_page<=1) return;
		if(trim($user_link)=="") $user_link="&laquo; Prev ";
	
		if(empty($link_para)) $link_para = $this->link_para;
		
		$html = "";
		if(!$this->is_first_page()&& $this->show_prev_next)
		{
			if(empty($this->jsHandler))
				$html = ' <a href="'.$this->page_name.'&page='.($this->page-1).$this->qry_str.'" '.$link_para.'>'.$user_link.'</a> ';
			else 
				$html = ' <a href="javascript:void(0);" onclick="'.$this->jsHandler.'(\''.$this->page_name.'\',\'page='.($this->page-1).$this->qry_str.'\')" '.$link_para.'>'.$user_link.'</a> ';
		}
		elseif($this->show_prev_next && $this->show_disabled_links)
			$html = $user_link;
	
		if($returnHtml) return $html;
		
		echo $html;
	
	}
	
	function get_scroll_prev_page_nav($user_link="",$link_para="",$returnHtml=false)
	{
		
		if($this->scroll_page>=$this->total_page) return;
		if(trim($user_link)=="") $user_link="Prev [$this->scroll_page]";
		
		$html = "";
		if($this->page>$this->scroll_page &&$this->show_scroll_prev_next)
		{
			if(empty($this->jsHandler))
				$html = ' <a href="'.$this->page_name.'&page='.($this->page-$this->scroll_page).$this->qry_str.'" '.$link_para.'>'.$user_link.'</a> ';
			else 
				$html = ' <a href="javascript:void(0);" onclick="'.$this->jsHandler.'(\''.$this->page_name.'\',\'page='.($this->page-$this->scroll_page).$this->qry_str.'\')" '.$link_para.'>'.$user_link.'</a> ';
		}
		elseif($this->show_scroll_prev_next && $this->show_disabled_links)
			$html = $user_link;
	
		if($returnHtml) return $html;
		
		echo $html;
	}
	
	function get_scroll_next_page_nav($user_link="",$link_para="",$returnHtml=false)
	{
		if($this->scroll_page>=$this->total_page)return;
		if(trim($user_link)=="") $user_link="Next[$this->scroll_page]";
	
		if(empty($link_para)) $link_para = $this->link_para;
		
		$html = "";
		if($this->total_page>$this->page+$this->scroll_page &&$this->show_scroll_prev_next)
		{
			if(empty($this->jsHandler))
				$html = ' <a href="'.$this->page_name.'&page='.($this->page+$this->scroll_page).$this->qry_str.'" '.$link_para.'>'.$user_link.'</a> ';
			else 
				$html = ' <a href="javascript:void(0);" onclick="'.$this->jsHandler.'(\''.$this->page_name.'\',\'page='.($this->page+$this->scroll_page).$this->qry_str.'\')" '.$link_para.'>'.$user_link.'</a> ';
		}
		elseif($this->show_scroll_prev_next && $this->show_disabled_links)
			$html = $user_link;
	
		if($returnHtml) return $html;
		
		echo $html;
	
	}
	
	function get_number_page_nav($link_para="",$returnHtml=false)
	{
		if(empty($link_para)) $link_para = $this->link_para;
		
		$j=0;
		$scroll_page=$this->scroll_page;
		if($this->page>($scroll_page/2))
		  $j=$this->page-intval($scroll_page/2);
		if($j+$scroll_page>$this->total_page)
		  $j=$this->total_page-$scroll_page;
		
		if($j<0)
			$i=0;
		else
			$i=$j;
		
		$html = "";
		for(;$i<$j+$scroll_page && $i<$this->total_records;$i++)
		{
			if($i==$this->page)
				$html .= ($i+1);
			else
			{
				if(empty($this->jsHandler))
					$html .= ' <a href="'.$this->page_name.'&page='.$i.$this->qry_str.'" '.$link_para.'>'.($i+1).'</a> ';
				else 
					$html .= ' <a href="javascript:void(0);" onclick="'.$this->jsHandler.'(\''.$this->page_name.'\',\'page='.$i.$this->qry_str.'\')" '.$link_para.'>'.($i+1).'</a> ';
			}
		}
	
		if($returnHtml) return $html;
		
		echo $html;
	
	}
	
	function get_page_nav($link_para="",$returnHtml=false)
	{
		if($this->total_records<=0)	return false;
		if($this->total_records<=$this->records_per_page) return "";
		if(!empty($link_para)) $this->link_para = $link_para;
	
		$html = "";
	
		$this->calculate();
		$html.= $this->get_first_page_nav("<img src='images/j_arrow_left_2.png' alt='first' border='0'>&nbsp;",$this->link_para,$returnHtml);
		//$html.= $this->get_scroll_prev_page_nav("<img src='images/j_arrow_left_2.png' alt='first' border='0'>&nbsp;",$this->link_para,$returnHtml);
		$html.= $this->get_prev_page_nav("<img src='images/j_arrow_left.png' alt='first' border='0'>&nbsp;&nbsp;",$this->link_para,$returnHtml);
		$html.= $this->get_number_page_nav($this->link_para,$returnHtml);
		$html.= $this->get_next_page_nav("&nbsp;&nbsp;<img src='images/j_arrow.png' alt='first' border='0'>",$this->link_para,$returnHtml);
		//$html.= $this->get_scroll_next_page_nav("&nbsp;<img src='images/j_arrow_2.png' alt='first' border='0'>",$this->link_para,$returnHtml);
		$html.= $this->get_last_page_nav("&nbsp;<img src='images/j_arrow_2.png' alt='first' border='0'>",$this->link_para,$returnHtml);
	
		if($returnHtml) return $html;
		return;
	}
	
	function calculate()
	{
		$this->page=$_REQUEST['page'];
		if(!is_numeric($this->page))
		  $this->page=0;
		$this->start=$this->page*$this->records_per_page;
		$this->total_page=@intval($this->total_records/$this->records_per_page);
		if($this->total_records%$this->records_per_page!=0)
		  $this->total_page++;
	}
	
	function get_limit_query($qry="")
	{
		$this->calculate();
		return $qry." LIMIT $this->start,$this->records_per_page";
	}
}

/*
* @function: outXML
* @Description: output XML to playlist.xml
* @Date: 12/01/2009
* @Author: tmvu.it@gmail.com
*/
function outXML(){
	$output[]=array();
	$output[]='<?xml version="1.0" encoding="utf-8"?>';
	$output[]='<song>';	
	$sql="SELECT * FROM vt_video ORDER BY date DESC";	
	$rs=mysql_query($sql);
	$i=1;
	while($row=mysql_fetch_array($rs)){		
		$output[]='<video>';
		$output[]='<id>'.$i.'</id>';
		$output[]='<title>'.$row["title"].'</title>';
		$output[]='<thumb></thumb>';
		$output[]='<source>http://www.actingcity.tv/crisqueur2/flv/8abbb92cd9d4a2b9db3d811b5c2a97eb/'.$row["video"].'.flv</source>';
		$output[]='<des>'.$row["description"].'</des>';
		$output[]='</video>';
		$i++;
	}
	$output[]='</song>';
	$r= join('',$output);	
	file_put_contents('xml/playlist.xml',$r);
}

/************* THE END ***************/	 	
?>